package com.mirror.utils.net;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;

import com.mirror.llk.utils.Constant;
import com.mirror.utils.base.BaseApplication;

/**
 * @Title: 网络检测工具类
 * @Version: 1.0
 * @Author: QiYin
 * @Date: 2013-2-19
 * @Company: EmagSoftware
 */
public class NetHandler {

	/**
	 * TAG
	 */
	private static final String TAG = "NetHandler";

	/**
	 * 检查网络连接
	 * 
	 * @param context
	 *            上下文
	 * @return 网络是否连接
	 */
	public static boolean doCheck(Context context) {
		Log.i(TAG, "检查网络连接情况");
		ConnectivityManager manager = (ConnectivityManager) BaseApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
		boolean isAvailable = manager.getActiveNetworkInfo() == null ? false : manager.getActiveNetworkInfo().isAvailable();
		Log.i(TAG, "网络连接:" + (isAvailable ? "正常" : "异常"));
		if (!isAvailable) {
			Log.i(TAG, "引导用户设置网络...");
			showSetup(context);
		}
		return isAvailable;
	}

	/**
	 * 检查网络连接
	 * 
	 * @param context
	 *            上下文
	 * @return 网络是否连接
	 */
	public static boolean doCheck() {
		Log.i(TAG, "检查网络连接情况");
		ConnectivityManager manager = (ConnectivityManager) BaseApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
		boolean isAvailable = manager.getActiveNetworkInfo() == null ? false : manager.getActiveNetworkInfo().isAvailable();
		Log.i(TAG, "网络连接:" + (isAvailable ? "正常" : "异常"));
		return isAvailable;
	}

	/**
	 * 获取本地IP地址
	 * 
	 * @return IP地址
	 */
	public static String getLocalIpAddress() {
		String ipaddress = Constant.EMPTY;
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						ipaddress = inetAddress.getHostAddress().toString();
					}
				}
			}
		}
		catch (SocketException e) {
			Log.e(TAG, Constant.SOCKET_EXCEPTION, e);
		}
		return ipaddress;
	}

	/**
	 * 显示设置对话框
	 * 
	 * @param context
	 *            上下文
	 */
	private static void showSetup(Context context) {
		Builder b = new AlertDialog.Builder(context).setTitle("没有可用的网络").setMessage("请开启GPRS或WIFI网络连接");
		b.setPositiveButton("确定", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {
				// 4.0以上版本变更使用不同的标志
				Intent mIntent = android.os.Build.VERSION.SDK_INT > 10 ? new Intent(android.provider.Settings.ACTION_SETTINGS) : new Intent(
				        android.provider.Settings.ACTION_WIRELESS_SETTINGS);
				// 新开启任务执行
				mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				BaseApplication.getContext().startActivity(mIntent);// 设置切换动画，从右边进入，左边退出
			}
		}).setNeutralButton("取消", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int whichButton) {
				dialog.cancel();
			}
		}).create();
		b.show();
	}

	public static void clearCookie(Context context) {
		CookieSyncManager.createInstance(context);
		CookieSyncManager.getInstance().startSync();
		CookieManager.getInstance().removeAllCookie();
		Log.i(TAG, "清除Cookie缓存完成");
	}
}
